package com.example.bbs.controller;


import com.example.bbs.mapper.UserInfoMapper;
import com.example.bbs.mapper.UserMapper;
import com.example.bbs.pojo.Result;
import com.example.bbs.pojo.User;
import com.example.bbs.utils.JwtUtil;
import com.example.bbs.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Api(tags = "员工相关接口")
public class UserContoller {
    //ioc容器 回收对象机制,只new一个
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/register")
    @ApiOperation(value = "员工登录")
    public Result register( String username, String password,String nickName) {
        if (username.length() <= 5 || username.length() >16){
            return Result.error("请确保格式正确");
        }else if(password.length() <= 5 || password.length() >16){
            return Result.error("请确保格式正确");
        }

        //查询用户
        User u = userMapper.findByUserName(username);
        if (u == null) {
            //没有占用
            //注册
            userMapper.add(username, password, nickName);
            userInfoMapper.insertUserInfoData(username);
            return Result.success();
        } else {
            //占用
            System.out.println("1");
            return Result.error("用户名已被占用");
        }
    }
    @PostMapping("/login")
    public Result login(String username, String password) {
        //根据用户名查询用户
        User loginUser = userMapper.findByUserName(username);
        //判断该用户是否存在
        if (loginUser == null) {
            return Result.error("用户名错误");
        }
        if(password.equals(loginUser.getPassword())){
            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getUser_id());
            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);

            ThreadLocalUtil.set(claims);
            //把token存储到redis中
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set(token,token,1, TimeUnit.HOURS);
            //把用户名存到线程(ThreadLocal是线程的局部变量,提供了线程隔离)中
            Map map = new HashMap<>();
            map.put("token",token);
            map.put("permission",loginUser.getPermission());
            return Result.success(map);
        }else {
        return Result.error("密码错误");}
    }

        @GetMapping("/userInfo")
    public Result userInfo(/*@RequestHeader(name = "Authorization") String token*/) {
        //根据用户名查询用户
       /* Map<String, Object> map = JwtUtil.parseToken(token);
        String username = (String) map.get("username");*/
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userMapper.findByUserName(username);
        return Result.success(user);
    }



}
